﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using System.ComponentModel;
using System.Collections.ObjectModel;
using Microsoft.Expression.Interactivity.Core;

namespace Intel_Client_GUI
{
    public class PlugInPageViewModel : INotifyPropertyChanged
    {
        public PlugInPageViewModel()
        {
            PluginCollection = new ObservableCollection<KeyValuePair<int, string>>(pm.PluginsSet); 
            //System.Windows.MessageBox.Show(PluginCollection.Count.ToString());
			pm.OnPluginSetChanged+=new PluginManager.PluginChangeHandler(pm_OnPluginSetChanged);
      		Globle.DateTimeData.OnTimeChanged+=new Intel_Client_GUI.DateTimeModel.DateTimeChangeHandler(DateTimeData_OnTimeChanged);  
		}
		~PlugInPageViewModel()
		{
			this.pm.OnPluginSetChanged-= this.pm_OnPluginSetChanged;
			Globle.DateTimeData.OnTimeChanged-=this.DateTimeData_OnTimeChanged;
		}
		
        public ObservableCollection<KeyValuePair<int, string>> PluginCollection
        {
            get;
            private set;
        }

        public Page SelectedPage
        {
            get { return this.selectPage; }
            set
            {
                this.selectPage = value;
                this.NotifyPropertyChanged("SelectedPage");
            }
        }

        private void SelectPage(object SelectItem)
        {
            if (SelectItem is KeyValuePair<int, string>)
            {
                var KeyPair = (KeyValuePair<int, string>)SelectItem;
                SelectedPage = pm.GetPage(KeyPair.Key);
            }
        }
		
		public object SelectItem
		{
			get{return this.selectItem;}
			set{this.selectItem = (KeyValuePair<int,string>)value;this.SelectPage(selectItem);}
		}
		
        private void pm_OnPluginSetChanged(int Uuid, string Name, PluginManager.PluginState s)
        {
            if (s == PluginManager.PluginState.Add)
            {
                this.PluginCollection.Add(new KeyValuePair<int, string>(Uuid, Name));
            }
            else
                this.PluginCollection.Remove(new KeyValuePair<int, string>(Uuid, Name));
        }

        private Page selectPage = null;

        private Intel_Client_GUI.PluginManager pm = Globle.Pm;

		private KeyValuePair<int,string> selectItem;

        #region INotifyPropertyChanged
        public event PropertyChangedEventHandler PropertyChanged;


        private void NotifyPropertyChanged(String info)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(info));
            }
        }

        private void DateTimeData_OnTimeChanged(Intel_Client_GUI.DateTimeModel.TimeInfo info)
        {
        	// 在此处添加事件处理程序实现。
			pm.ChangeTimeScale(this.selectItem.Key,Globle.DateTimeData.TimeAndDuration.start,Globle.DateTimeData.TimeAndDuration.duration);
        }
        #endregion
    }

}
